/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sadhar.sipp.laporanrkaunit;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;
import org.sadhar.sia.framework.ClassApplicationModule;

import org.zkoss.zul.Button;

import org.sadhar.sia.common.ClassSession;
import org.sadhar.sipp.unkerja.UnKerja;
import org.sadhar.sipp.unkerja.UnKerjaDAO;
import org.sadhar.sipp.unkerja.UnKerjaDAOImpl;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Session;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zkex.zul.Jasperreport;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Row;
import org.zkoss.zul.Tabbox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

/**
 *
 * @author Yohan Hardinugroho
 * --revised by dewi (BAPSI)
 */
public class LaporanRKAUnitWnd extends ClassApplicationModule {

    private Label lblTahunAnggaran;
    private Label lblUnitKerja;
    private Label lblUnitKerja2;
    private Textbox txtboxKeySearch;
    private Textbox txtboxKd_unit_kerja;
    private Button btnCari;
    private Button buttonHideExport;
    private Jasperreport report;
    private Jasperreport report2;
    private Listbox lstboxFormatLaporan;
    private Tabbox tbboxLaporan;
    private Row rowUnitForUnit;
    private Row rowPilihUnit;
    private Row rowUnitForBK;
    private ClassSession session;
    private LaporanRKAUnitDAO laporanRKAUnitDAO;
    private UnKerjaDAO unKerjaDAO;
    private boolean keuangan = false;
    private Session sesi;

    public LaporanRKAUnitWnd() {
        sesi = Sessions.getCurrent();
        session = (ClassSession) sesi.getAttribute("CURRENT_SESSION");
        laporanRKAUnitDAO = new LaporanRKAUnitDAOImpl();
        unKerjaDAO = new UnKerjaDAOImpl();
    }

    public void onCreate() throws Exception {

        lblTahunAnggaran = (Label) getFellow("lblTahunAnggaran");
        lblUnitKerja = (Label) getFellow("lblUnitKerja");
        lblUnitKerja2 = (Label) getFellow("lblUnitKerja2");
        txtboxKeySearch = (Textbox) getFellow("txtboxKeySearch");
        txtboxKd_unit_kerja = (Textbox) getFellow("txtboxKd_unit_kerja");
        tbboxLaporan = (Tabbox) getFellow("tbboxLaporan");
        report = (Jasperreport) getFellow("report");
        report2 = (Jasperreport) getFellow("report2");
        rowUnitForUnit = (Row) getFellow("rowUnitForUnit");
        rowPilihUnit = (Row) getFellow("rowPilihUnit");
        rowUnitForBK = (Row) getFellow("rowUnitForBK");
        lstboxFormatLaporan = (Listbox) getFellow("lstboxFormatLaporan");
        btnCari = (Button) getFellow("btnCari");
        buttonHideExport = (Button) getFellow("buttonHideExport");
        lblTahunAnggaran.setValue(session.getThnAnggaran());
        UnKerja uk = unKerjaDAO.select(session.getKodeUnit());
        lblUnitKerja.setValue(uk.getNamaUnitKerja());
        load();
    }

    public void load() {
        String kodeUnit = session.getKodeUnit();
        int compare = kodeUnit.compareTo("08000000");

        if (compare == 0) {
            rowUnitForBK.setVisible(true);
            rowPilihUnit.setVisible(true);
            rowUnitForUnit.setVisible(false);
            keuangan = true;

        } else {
            rowUnitForBK.setVisible(false);
            rowPilihUnit.setVisible(false);
            rowUnitForUnit.setVisible(true);
            keuangan = false;
        }
    }

    public void showExport() {
        tbboxLaporan.setVisible(true);
        buttonHideExport.setVisible(true);
    }

    public void btnCariOnClick() throws InterruptedException {
        final Window win = (Window) Executions.createComponents(
                "sippzul/laporanrkaunit/UnitKerjaPopup.zul", this,
                null);
        Textbox txtboxSearch = (Textbox) win.getFellow("txtboxSearch");
        txtboxSearch.setValue(txtboxKeySearch.getValue());
        win.doModal();

        txtBoxKdUnitKerjaOnChange();
    }

    public void txtBoxKdUnitKerjaOnChange() {
        lblUnitKerja.setValue(lblUnitKerja2.getValue());
        if (txtboxKeySearch.getValue().trim().isEmpty()) {
            txtboxKd_unit_kerja.setValue("");
        }
        txtboxKeySearch.setValue("");

    }

    public void hideExport() {
        tbboxLaporan.setVisible(false);
        buttonHideExport.setVisible(false);
    }

    public class RKA2AllCustomDataSource implements JRDataSource {

        private Object[][] data;
        private int index = -1;

        public RKA2AllCustomDataSource() throws Exception {
            String tahun = session.getThnAnggaran();
            String tahunKelompok = laporanRKAUnitDAO.tahunKelompokAnggaranYangDigunakan(session.getThnAnggaran());
            data = laporanRKAUnitDAO.SetDataToCetak2All(tahun, tahunKelompok);
        }

        public boolean next() throws JRException {
            index++;

            return (index < data.length);
        }

        public Object getFieldValue(JRField field) throws JRException {
            Object value = null;

            String fieldName = field.getName();
            LinkedList xLs;
            try {
                xLs = laporanRKAUnitDAO.getFieldCln();
                for (int i = 0; i < xLs.size(); i++) {
                    if (xLs.get(i).equals(fieldName)) {
                        value = data[index][i];
                    }
                }
            } catch (Exception ex) {
                Logger.getLogger(LaporanRKAUnit.class.getName()).log(Level.SEVERE, null, ex);
            }
            return value;
        }
    }

    public class RKAAllCustomDataSource implements JRDataSource {

        private Object[][] data;
        private int index = -1;

        public RKAAllCustomDataSource() throws Exception {
            String tahun = session.getThnAnggaran();
            String tahunKelompok = laporanRKAUnitDAO.tahunKelompokAnggaranYangDigunakan(session.getThnAnggaran());
            data = laporanRKAUnitDAO.SetDataToCetakRKAall(tahun, tahunKelompok);
        }

        public boolean next() throws JRException {
            index++;

            return (index < data.length);
        }

        public Object getFieldValue(JRField field) throws JRException {
            Object value = null;

            String fieldName = field.getName();
            LinkedList xLs;
            try {
                xLs = laporanRKAUnitDAO.getFieldCln();
                for (int i = 0; i < xLs.size(); i++) {
                    if (xLs.get(i).equals(fieldName)) {
                        value = data[index][i];
                    }
                }
            } catch (Exception ex) {
                Logger.getLogger(LaporanRKAUnit.class.getName()).log(Level.SEVERE, null, ex);
            }
            return value;
        }
    }

    public class RKA2CustomDataSource implements JRDataSource {

        private Object[][] data;
        private int index = -1;

        public RKA2CustomDataSource() throws Exception {
            String tahun = session.getThnAnggaran();
            String tahunKelompok = laporanRKAUnitDAO.tahunKelompokAnggaranYangDigunakan(session.getThnAnggaran());
            String kdUnitKerja = "";
            if (keuangan == true) {
                kdUnitKerja = txtboxKd_unit_kerja.getValue();
            } else {
                kdUnitKerja = session.getKodeUnit();
            }
            data = laporanRKAUnitDAO.SetDataToCetakRKA2(tahun, tahunKelompok, kdUnitKerja);
        }

        public boolean next() throws JRException {
            index++;

            return (index < data.length);
        }

        public Object getFieldValue(JRField field) throws JRException {
            Object value = null;

            String fieldName = field.getName();
            LinkedList xLs;
            try {
                xLs = laporanRKAUnitDAO.getFieldCln();
                for (int i = 0; i < xLs.size(); i++) {
                    if (xLs.get(i).equals(fieldName)) {
                        value = data[index][i];
                    }
                }
            } catch (Exception ex) {
                Logger.getLogger(LaporanRKAUnit.class.getName()).log(Level.SEVERE, null, ex);
            }
            return value;
        }
    }

    public class RKACustomDataSource implements JRDataSource {

        private Object[][] data;
        private int index = -1;

        public RKACustomDataSource() throws Exception {
            String tahun = session.getThnAnggaran();
            String tahunKelompok = laporanRKAUnitDAO.tahunKelompokAnggaranYangDigunakan(session.getThnAnggaran());
            String kdUnitKerja = "";
            if (keuangan == true) {
                kdUnitKerja = txtboxKd_unit_kerja.getValue();
            } else {
                kdUnitKerja = session.getKodeUnit();
            }
            data = laporanRKAUnitDAO.SetDataToCetakRKA(tahun, tahunKelompok, kdUnitKerja);
        }

        public boolean next() throws JRException {
            index++;

            return (index < data.length);
        }

        public Object getFieldValue(JRField field) throws JRException {
            Object value = null;

            String fieldName = field.getName();
            LinkedList xLs;
            try {
                xLs = laporanRKAUnitDAO.getFieldCln();
                for (int i = 0; i < xLs.size(); i++) {
                    if (xLs.get(i).equals(fieldName)) {
                        value = data[index][i];
                    }
                }
            } catch (Exception ex) {
                Logger.getLogger(LaporanRKAUnit.class.getName()).log(Level.SEVERE, null, ex);
            }
            return value;
        }
    }

    public class IndikatorCustomDataSource implements JRDataSource {

        private Object[][] data;
        private int index = -1;

        public IndikatorCustomDataSource() throws Exception {
            String tahun = session.getThnAnggaran();
            String kdUnitKerja = "";
            if (keuangan == true) {
                kdUnitKerja = txtboxKd_unit_kerja.getValue();
            } else {
                kdUnitKerja = session.getKodeUnit();
            }
            data = laporanRKAUnitDAO.SetDataToCetakInd(tahun, kdUnitKerja);
        }

        public boolean next() throws JRException {
            index++;

            return (index < data.length);
        }

        public Object getFieldValue(JRField field) throws JRException {
            Object value = null;

            String fieldName = field.getName();
            LinkedList xLs;
            try {
                xLs = laporanRKAUnitDAO.getFieldCln1();
                for (int i = 0; i < xLs.size(); i++) {
                    if (xLs.get(i).equals(fieldName)) {
                        value = data[index][i];
                    }
                }
            } catch (Exception ex) {
                Logger.getLogger(LaporanRKAUnit.class.getName()).log(Level.SEVERE, null, ex);
            }
            return value;
        }
    }

    public class IndikatorAllCustomDataSource implements JRDataSource {

        private Object[][] data;
        private int index = -1;

        public IndikatorAllCustomDataSource() throws Exception {
            String tahun = session.getThnAnggaran();
            data = laporanRKAUnitDAO.SetDataToCetakIndAll(tahun);
        }

        public boolean next() throws JRException {
            index++;

            return (index < data.length);
        }

        public Object getFieldValue(JRField field) throws JRException {
            Object value = null;

            String fieldName = field.getName();
            LinkedList xLs;
            try {
                xLs = laporanRKAUnitDAO.getFieldCln1();
                for (int i = 0; i < xLs.size(); i++) {
                    if (xLs.get(i).equals(fieldName)) {
                        value = data[index][i];
                    }
                }
            } catch (Exception ex) {
                Logger.getLogger(LaporanRKAUnit.class.getName()).log(Level.SEVERE, null, ex);
            }
            return value;
        }
    }

    public void export() throws Exception {

        String cek = "";
        String tahunKelompok = laporanRKAUnitDAO.tahunKelompokAnggaranYangDigunakan(session.getThnAnggaran());
        String kdUnitKerja = txtboxKd_unit_kerja.getValue();
        if (keuangan == true) {
            String unker = lblUnitKerja2.getValue();
            if (unker.compareTo("...........") == 0) {
                List lscari1 = laporanRKAUnitDAO.getQueryRKA11All(session.getThnAnggaran(), tahunKelompok);
                for (Iterator it = lscari1.iterator(); it.hasNext();) {
                    LaporanRKAUnit data = (LaporanRKAUnit) it.next();
                    cek = data.getNama_unit_kerja();
                }
                if (cek == null) // cek awal kosong
                {
                    List lscari8 = laporanRKAUnitDAO.getQueryRKA22All(session.getThnAnggaran(), tahunKelompok);
                    for (Iterator it = lscari8.iterator(); it.hasNext();) {
                        LaporanRKAUnit data = (LaporanRKAUnit) it.next();
                        cek = data.getNama_unit_kerja();
                    }
                    if (cek == null) // cek dua kosong
                    {
                        Messagebox.show("Belum ada anggaran yang dibuat", "Peringatan", Messagebox.OK, Messagebox.ERROR);
                    } else {
                        Map m = new HashMap();
                        m.put("tahunKelompok", tahunKelompok);
                        m.put("tahun", session.getThnAnggaran());
                        report.setParameters(m);
                        report.setType(lstboxFormatLaporan.getSelectedItem().getValue().toString());
                        report.setSrc("reports/laporanrkaunit/LaporanRKAUnit_all_2.jasper");
                        report.setDatasource(new RKA2AllCustomDataSource());

                        report2.setParameters(m);
                        report2.setType(lstboxFormatLaporan.getSelectedItem().getValue().toString());
                        report2.setSrc("reports/laporanrkaunit/DaftarIndikatorKinerja_all.jasper");
                        report2.setDatasource(new IndikatorAllCustomDataSource());
                        showExport();
                    }
                } else {
                    Map m = new HashMap();
                    m.put("tahunKelompok", tahunKelompok);
                    m.put("tahun", session.getThnAnggaran());
                    report.setParameters(m);
                    report.setType(lstboxFormatLaporan.getSelectedItem().getValue().toString());
                    report.setSrc("reports/laporanrkaunit/LaporanRKAUnit_all_2.jasper");
                    report.setDatasource(new RKAAllCustomDataSource());

                    report2.setParameters(m);
                    report2.setType(lstboxFormatLaporan.getSelectedItem().getValue().toString());
                    report2.setSrc("reports/laporanrkaunit/DaftarIndikatorKinerja_all.jasper");
                    report2.setDatasource(new IndikatorAllCustomDataSource());
                    showExport();
                }
            } else {
                List lscari2 = laporanRKAUnitDAO.getQueryRKA11(session.getThnAnggaran(), tahunKelompok, kdUnitKerja);
                for (Iterator it = lscari2.iterator(); it.hasNext();) {
                    LaporanRKAUnit data = (LaporanRKAUnit) it.next();
                    cek = data.getNama_unit_kerja();
                }
                if (cek == null) {
                    List lscari9 = laporanRKAUnitDAO.getQueryRKA22(session.getThnAnggaran(), tahunKelompok, kdUnitKerja);
                    for (Iterator it = lscari9.iterator(); it.hasNext();) {
                        LaporanRKAUnit data = (LaporanRKAUnit) it.next();
                        cek = data.getNama_unit_kerja();
                    }
                    if (cek == null) // cek dua kosong
                    {
                        Messagebox.show("Belum ada anggaran yang dibuat", "Peringatan", Messagebox.OK, Messagebox.ERROR);
                    } else {
                        Map m = new HashMap();
                        m.put("tahunKelompok", tahunKelompok);
                        m.put("tahun", session.getThnAnggaran());
                        m.put("kodeUnit", kdUnitKerja);
                        report.setParameters(m);
                        report.setType(lstboxFormatLaporan.getSelectedItem().getValue().toString());
                        report.setSrc("reports/laporanrkaunit/LaporanRKAUnit_e_2.jasper");
                        report.setDatasource(new RKA2CustomDataSource());

                        report2.setParameters(m);
                        report2.setType(lstboxFormatLaporan.getSelectedItem().getValue().toString());
                        report2.setSrc("reports/laporanrkaunit/DaftarIndikatorKinerja_e.jasper");
                        report2.setDatasource(new IndikatorCustomDataSource());
                        showExport();
                    }

                } else {
                    Map m = new HashMap();
                    m.put("tahunKelompok", tahunKelompok);
                    m.put("tahun", session.getThnAnggaran());
                    m.put("kodeUnit", kdUnitKerja);
                    report.setParameters(m);
                    report.setType(lstboxFormatLaporan.getSelectedItem().getValue().toString());
                    report.setSrc("reports/laporanrkaunit/LaporanRKAUnit_e.jasper");
                    report.setDatasource(new RKACustomDataSource());

                    report2.setParameters(m);
                    report2.setType(lstboxFormatLaporan.getSelectedItem().getValue().toString());
                    report2.setSrc("reports/laporanrkaunit/DaftarIndikatorKinerja_e.jasper");
                    report2.setDatasource(new IndikatorCustomDataSource());
                    showExport();
                }
            }
        } else {
            kdUnitKerja = session.getKodeUnit();
            List lscari2 = laporanRKAUnitDAO.getQueryRKA11(session.getThnAnggaran(), tahunKelompok, kdUnitKerja);
            for (Iterator it = lscari2.iterator(); it.hasNext();) {
                LaporanRKAUnit data = (LaporanRKAUnit) it.next();
                cek = data.getNama_unit_kerja();
            }
            if (cek == null) {
                List lscari5 = laporanRKAUnitDAO.getQueryRKA22(session.getThnAnggaran(), tahunKelompok, kdUnitKerja);
                for (Iterator it = lscari5.iterator(); it.hasNext();) {
                    LaporanRKAUnit data = (LaporanRKAUnit) it.next();
                    cek = data.getNama_unit_kerja();
                }
                if (cek == null) // cek dua kosong
                {
                    Messagebox.show("Belum ada anggaran yang dibuat", "Peringatan", Messagebox.OK, Messagebox.ERROR);
                } else {
                    Map m = new HashMap();
                    m.put("tahunKelompok", tahunKelompok);
                    m.put("tahun", session.getThnAnggaran());
                    m.put("kodeUnit", kdUnitKerja);
                    report.setParameters(m);
                    report.setType(lstboxFormatLaporan.getSelectedItem().getValue().toString());
                    report.setSrc("reports/laporanrkaunit/LaporanRKAUnit_e_2.jasper");
                    report.setDatasource(new RKA2CustomDataSource());

                    report2.setParameters(m);
                    report2.setType(lstboxFormatLaporan.getSelectedItem().getValue().toString());
                    report2.setSrc("reports/laporanrkaunit/DaftarIndikatorKinerja_e.jasper");
                    report2.setDatasource(new IndikatorCustomDataSource());
                    showExport();
                }
            } else {
                Map m = new HashMap();
                m.put("tahunKelompok", tahunKelompok);
                m.put("tahun", session.getThnAnggaran());
                m.put("kodeUnit", kdUnitKerja);
                report.setParameters(m);
                report.setType(lstboxFormatLaporan.getSelectedItem().getValue().toString());
                report.setSrc("reports/laporanrkaunit/LaporanRKAUnit_e.jasper");
                report.setDatasource(new RKACustomDataSource());

                report2.setParameters(m);
                report2.setType(lstboxFormatLaporan.getSelectedItem().getValue().toString());
                report2.setSrc("reports/laporanrkaunit/DaftarIndikatorKinerja_e.jasper");
                report2.setDatasource(new IndikatorCustomDataSource());
                showExport();
            }
        }
    }
}

